package ge.telasi.tasks.ui.structure;

import ge.telasi.tasks.model.Structure;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.SimpleTable;
import ge.telasi.tasks.ui.comp.TableModel;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;

/**
 * Structure table.
 *
 * @author dimitri
 */
public class StructureTable extends SimpleTable<Structure> {
    private static final long serialVersionUID = 9186388616991812892L;

    static final int IMAGE = 0;
    static final int NAME = 1;
    static final int TYPE = 2;

    private List<Structure> structures = new ArrayList<Structure>();

    public List<Structure> getStructures() {
        return structures;
    }

    public void setStructures(List<Structure> structures) {
        this.structures = structures;
        refresh();
    }
    
    @Override
    protected TableModel<Structure> createEmptyModel() {
        return new StructureTableModel();
    }

    @Override
    protected List<Structure> getDataFromServer(Platform pl) throws Exception {
        return structures;
    }

    @Override
    protected String getPreferenceName() {
        return "03282489829201339911";
    }

    @Override
    protected int[] getDefaultColumnWidthes() {
        return new int[] {20, 250, 150};
    }

    class StructureTableModel extends TableModel<Structure> {
        private static final long serialVersionUID = -5112307566340196838L;

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            switch(columnIndex) {
                case IMAGE:
                    return ImageIcon.class;
                case NAME:
                case TYPE:
                    return String.class;
                default:
                    return Object.class;
            }
        }

        @Override
        public String getColumnName(int column) {
            switch(column) {
                case IMAGE:
                    return "";
                case NAME:
                    return "დასახელება";
                case TYPE:
                    return "ტიპი";
                default:
                    return "?";
            }
        }

        @Override
        public Object getValueAt(int row, int column) {
            Structure struct = getObjectAt(row);
            switch(column) {
                case IMAGE:
                    return StructureUtils.getIcon(struct.getType());
                case NAME:
                    return struct.getName();
                case TYPE:
                    return StructureUtils.getName(struct.getType());
                default:
                    return null;
            }
        }
    }

}
